package cn.rui.service.impl;

import cn.rui.entity.PageResult;
import cn.rui.entity.QueryPageBean;
import cn.rui.entity.Result;
import cn.rui.mapper.MenuMapper;
import cn.rui.mapper.RoleMapper;
import cn.rui.pojo.Menu;
import cn.rui.pojo.Role;
import cn.rui.service.MenuService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 徽州大都督
 * @date 2020/9/2
 */
@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 菜单分页带条件查询
     *
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage ();
        Integer pageSize = queryPageBean.getPageSize ();
        String queryString = queryPageBean.getQueryString ();

        PageHelper.startPage (currentPage, pageSize);

        Page<Menu> page = menuMapper.selectByCondition (queryString);
        long total = page.getTotal ();
        List<Menu> menuList = page.getResult ();
        return new PageResult (total, menuList);
    }

    /**
     * 查询主菜单名称集合
     *
     * @return
     */
    public List<String> findMainMenuName() {
        return menuMapper.findMainMenuName ();
    }

    /**
     * 添加菜单项
     *
     * @param map
     * @return
     */
    public void handleAdd(Map map) {

        Menu menu = new Menu ();
        menu.setName ((String) map.get ("name"));
        menu.setLinkUrl ((String) map.get ("linkUrl"));

        //根据菜单名称查询主菜单
        Menu mainMenu = menuMapper.findMainMenuByName ((String) map.get ("MainName"));
        menu.setParentMenuId (mainMenu.getId ());
        menu.setPriority (Integer.parseInt (map.get ("priority").toString ()) );
        menu.setIcon ((String) map.get ("icon"));
        menu.setDescription ((String) map.get ("attention"));
        menu.setPath ("/" + mainMenu.getPath () + "-" + (String) map.get ("path"));
        menu.setLevel (2);
        menuMapper.add(menu);

        Integer menuId = menu.getId ();
        //查询ROLE_ADMIN的id
        Role role=roleMapper.findByKeyWord("ROLE_ADMIN");
        Map map1 = new HashMap ();
        map1.put ("role_id",role.getId ());
        map1.put ("menu_id",menuId);
        roleMapper.addRoleAndMenu(map1);
    }

    /**
     * 根据id查询菜单项
     * @param id
     */
    public void deleteById(Integer id) {

        //根据菜单id删除role关联的菜单
        roleMapper.deleteByMenuId(id);

        //删除菜单
        menuMapper.deleteById(id);
    }

    /**
     * 根据id查询菜单信息
     * @param id
     * @return
     */
    public Map findById(Integer id) {

        Menu menu =menuMapper.findById(id);

        Map<String,Object> map =new HashMap<> ();
        map.put ("name",menu.getName ());
        map.put ("linkUrl",menu.getLinkUrl ());
        map.put ("path",menu.getPath ());
        map.put ("priority",menu.getPriority ());

        if (menu.getParentMenuId ()!=null){
            Menu mainMenu = menuMapper.findById (menu.getParentMenuId ());

            map.put ("MainName",mainMenu.getName ());
        }

        map.put ("icon",menu.getIcon ());
        map.put ("attention",menu.getDescription ());
        map.put ("id",id);
        return map;
    }

    /**
     * 主菜单分页查询
     */
    public PageResult findMainPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage ();
        Integer pageSize = queryPageBean.getPageSize ();
        String queryString = queryPageBean.getQueryString ();
        PageHelper.startPage (currentPage,pageSize);

        Page<Menu> page=menuMapper.findMainPage(queryString);

        long total = page.getTotal ();
        List<Menu> result = page.getResult ();
        return new PageResult(total,result);
    }

    /**
     * 添加主菜单
     */
    public void MainMenuAdd(Menu menu) {

        menu.setLevel (1);
        menuMapper.add (menu);
        Integer menuId = menu.getId ();
        Role role = roleMapper.findByKeyWord ("ROLE_ADMIN");
        Map<String,Object> map = new HashMap<> ();
        map.put ("role_id",role.getId ());
        map.put ("menu_id",menuId);
        roleMapper.addRoleAndMenu (map);
    }

    /**
     * 编辑菜单
     */
    public void handleEdit(Map map) {
        Menu menu = new Menu ();
        menu.setId (Integer.parseInt (map.get ("id").toString ()));
        menu.setName ((String) map.get ("name"));
        menu.setLinkUrl ((String) map.get ("linkUrl"));
        menu.setPath ((String) map.get ("path"));
        menu.setPriority (Integer.parseInt (map.get ("priority").toString ()));
        menu.setDescription ((String) map.get ("attention"));
        menu.setIcon ((String) map.get ("icon"));

        Menu mainName = menuMapper.findMainMenuByName ((String) map.get ("MainName"));
        menu.setParentMenuId (mainName.getId ());
        menuMapper.handleEdit(menu);
    }

    /**
     * 编辑主菜单
     */
    public void MainMenuEdit(Menu menu) {
        //编辑
        menuMapper.handleEdit (menu);

    }

    /**
     * 根据role_id查询所有菜单权限，并且排序
     * @return
     */
    public List<Menu> findAllMenByRole() {


        List<Menu> page = menuMapper.findAllMenByRole();
        return page;
    }
}
